package com.wfy.modules.sys.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.wfy.modules.sys.entity.Menu;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/5/20-20:35
 */
public class MenuVo implements Serializable {

    // ================================================================
    // Constants
    // ================================================================

    private static final long serialVersionUID = 1L;

    // ================================================================
    // Fields
    // ================================================================

    private String id;
    private String parentId;
    private String text;
    private String href;
    private String icon;
    private String i18n;
    private String permission;
    private String type;
    private int sort;
    private boolean hide;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;
    private String createBy;
    private String remarks;
    private boolean group;

    private List<MenuVo> children;

    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    @Override
    public String toString() {
        return "MenuVo{" +
                "id='" + id + '\'' +
                ", parentId='" + parentId + '\'' +
                ", text='" + text + '\'' +
                ", href='" + href + '\'' +
                ", icon='" + icon + '\'' +
                ", i18n='" + i18n + '\'' +
                ", permission='" + permission + '\'' +
                ", type='" + type + '\'' +
                ", sort=" + sort +
                ", hide=" + hide +
                ", createDate=" + createDate +
                ", createBy='" + createBy + '\'' +
                ", remarks='" + remarks + '\'' +
                ", group=" + group +
                ", children=" + children +
                '}';
    }


    // ================================================================
    // Public or Protected Methods
    // ================================================================

    /**
     * 根据菜单构造返回数据
     *
     * @param menus
     * @return
     */
    public List<MenuVo> build(List<Menu> menus) {
        if (null == menus || menus.isEmpty()) {
            return Collections.emptyList();
        }
        final List<MenuVo> menuVos = new ArrayList<>(menus.size());
        for (Menu m : menus) {
            menuVos.add(build(m));
        }
        return menuVos;
    }

    public MenuVo build(Menu menu) {
        MenuVo vo = new MenuVo();
        BeanUtils.copyProperties(menu, vo);
        String isShow = menu.getIsShow();
        if (Objects.equals(isShow, "1")) {
            vo.setHide(false);
        } else {
            vo.setHide(true);
        }
        vo.setGroup(true);
        String name = menu.getName();
        vo.setText(name);
        return vo;
    }

    /**
     * 生成菜单树
     *
     * @param items
     * @return
     */
    public List<MenuVo> merge(List<Menu> items) {
        List<MenuVo> vos = build(items);
        List<MenuVo> result = new ArrayList<>();
        for (MenuVo vo : vos) {
            if (vo.getParentId().equals("0")) {
                result.add(vo);
            }
        }
        for (int i = 0; i < result.size(); i++) {
            MenuVo vo = result.get(i);
            vo.setChildren(getChildren(vos, vo.getId()));
        }
        return result;
    }

    private List<MenuVo> getChildren(List<MenuVo> menuVos, String pid) {
        List<MenuVo> children = new ArrayList<>();
        for (int i = 0; i < menuVos.size(); i++) {
            MenuVo vo = menuVos.get(i);
            if (pid.equals(vo.getParentId())) {
                children.add(vo);
            }
        }
        return children;
    }

    // ================================================================
    // Getter & Setter
    // ================================================================

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getI18n() {
        return i18n;
    }

    public void setI18n(String i18n) {
        this.i18n = i18n;
    }

    public boolean isHide() {
        return hide;
    }

    public void setHide(boolean hide) {
        this.hide = hide;
    }

    public boolean isGroup() {
        return group;
    }

    public void setGroup(boolean group) {
        this.group = group;
    }

    public List<MenuVo> getChildren() {
        return children;
    }

    public void setChildren(List<MenuVo> children) {
        this.children = children;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    // ================================================================
    // Private Methods
    // ================================================================

    /**
     * 获取根菜单
     *
     * @param menus
     * @return
     */
    private List<MenuVo> getRoot(List<MenuVo> menus) {
        List<MenuVo> roots = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            MenuVo m = menus.get(i);
            if ("0".equals(m.getParentId())) {
                roots.add(m);
            }
        }
        return roots;
    }

    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
